#!/bin/bash
HOME="/home"
PHP_INSTALL_DIR="/usr/local"
PHP_INSTALL="php73"
PHP_FLOD="php-7.3.27"
PHP_TAR="$PHP_FLOD.tar.gz"
PHP_URL="http://mirrors.sohu.com/php/$PHP_TAR"
#创建用户及用户组方法
function createUserGroupAndUser() {
    group=$1
    user=$2
    egrep "^$group" /etc/group >&/dev/null
    #创建用户组
    if [[ $? -ne 0 ]]; then
        groupadd $group
    fi
    #创建用户组下面的用户
    egrep "^$user" /etc/passwd >&/dev/null
    if [ $? -ne 0 ]; then
        useradd -r -s /sbin/nologin -g $group $user
    fi
}
function installPHP() {
    #yum安装依赖的包
    rpm -q yum &>/dev/null
    if [ $? -ne 0 ]; then
        echo "请先安装yum工具"
        exit
    fi
    rpm -q wget &>/dev/null
    if [ $? -ne 0 ]; then
        yum install wget -y
    fi
    rpm -q gcc &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y gcc
    fi
    rpm -q gcc-c++ &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y gcc-c++
    fi
    rpm -q gd-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y make gd-devel
    fi
    rpm -q libxml2-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y make libxml2-devel
    fi
    rpm -q libcurl-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y make libcurl-devel
    fi
    rpm -q libjpeg-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y make libjpeg-devel
    fi

    yum install -y make libwebp-devel
    yum install bzip2 bzip2-devel -y

    rpm -q libpng-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y make libpng-devel
    fi
    rpm -q openssl-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y make openssl-devel
    fi
    rpm -q libmcrypt-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y make libmcrypt-devel
    fi
    rpm -q libxslt-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y make libxslt-devel
    fi
    rpm -q libtidy-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y make libtidy-devel
    fi
    rpm -q sqlite-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y make sqlite-devel
    fi
    rpm -q oniguruma &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y "http://mirrors.sohu.com/centos/7/cloud/x86_64/openstack-queens/Packages/o/oniguruma-6.7.0-1.el7.x86_64.rpm"
    fi
    rpm -q oniguruma-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install -y "http://mirrors.sohu.com/centos/7/cloud/x86_64/openstack-queens/Packages/o/oniguruma-devel-6.7.0-1.el7.x86_64.rpm"
    fi

    if [ ! -d "$HOME" ]; then
        mkdir $HOME
    fi

    cd "$HOME"
    #下载镜像并解压Nginx源码包
    if [ ! -f "$HOME/$PHP_TAR" ]; then
        wget -c $PHP_URL && tar -zxf $PHP_TAR
    fi
    cd $PHP_FLOD
    #创建用户组
    createUserGroupAndUser www www
    #预编译
    ./configure --prefix=$PHP_INSTALL_DIR/$PHP_INSTALL \
    --with-config-file-path=$PHP_INSTALL_DIR/$PHP_INSTALL/etc \
    --enable-fpm --enable-opcache \
    --with-fpm-user=www --with-fpm-group=www \
    --with-mysqli --enable-pdo --with-pdo-mysql \
    --with-openssl --with-curl --with-bz2 \
    --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-webp-dir --with-xpm-dir --with-zlib --with-zlib-dir \
    --enable-mbstring --enable-hash --enable-calendar --enable-sockets --disable-debug --disable-rpath \
    --enable-pcntl --enable-posix

    if [ $? -ne 0 ]; then
        echo "php预编译失败"
        exit
    else
        echo "php预编译成功"
    fi
    make clean && make -j 4 && make install
    if [ $? -ne 0 ]; then
        echo "make或make install失败"
        exit
    else
        echo "make成功"
    fi
    #软连接
    ln -s $PHP_INSTALL_DIR/$PHP_INSTALL/bin/php /usr/bin/php
    #配置文件的操作
    cp php.ini-production $PHP_INSTALL_DIR/$PHP_INSTALL/etc/php.ini #php的配置文件
    cp sapi/fpm/php-fpm.conf $PHP_INSTALL_DIR/$PHP_INSTALL/etc/php-fpm.conf
    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm #php的自动启动
    cp $PHP_INSTALL_DIR/$PHP_INSTALL/etc/php-fpm.d/www.conf.default $PHP_INSTALL_DIR/$PHP_INSTALL/etc/php-fpm.d/www.conf
    chmod +x /etc/init.d/php-fpm
    chkconfig php-fpm on #添加到开机启动
}
installPHP
